package com.situ.mall.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.situ.mall.common.entity.Cart;
import com.situ.mall.common.entity.Member;
import com.situ.mall.common.entity.OrderDetail;
import com.situ.mall.common.entity.Product;
import com.situ.mall.common.mapper.CartMapper;
import com.situ.mall.common.mapper.OrderDetailMapper;
import com.situ.mall.common.mapper.ProductMapper;
import com.situ.mall.common.service.IOrderDetailService;
import com.situ.mall.common.util.RequestUtil;
import com.situ.mall.common.vo.AllPriceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lwj
 * @since 2023-09-19
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements IOrderDetailService {


    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductMapper productMapper;
    @Override
    public AllPriceVO allPrice(String ids) {
        Member m=(Member) RequestUtil.getCurrent();
        AllPriceVO addprice=new AllPriceVO();
        BigDecimal sum = new BigDecimal("0");
        for (String pid : ids.split(",")){
            Cart c =  cartMapper.selectByproductid(Integer.parseInt(pid),m.getId());
            Product p = productMapper.selectById(c.getProductId());
            sum = sum.add(p.getPrice().multiply(new BigDecimal(c.getAmount())));
        }
        addprice.setSum(sum);
        return addprice;
    }

    @Override
    public AllPriceVO onePrice(Integer id) {
        Member m=(Member) RequestUtil.getCurrent();
        AllPriceVO addprice=new AllPriceVO();
        BigDecimal sum = new BigDecimal("0");
        Cart c =  cartMapper.selectByproductid(id,m.getId());
        Product p = productMapper.selectById(c.getProductId());
        sum = sum.add(p.getPrice().multiply(new BigDecimal(c.getAmount())));
        addprice.setSum(sum);
        return addprice;
    }
}
